package com.purong.purong.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.purong.purong.PermissionConstant;
import com.purong.purong.R;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.TakePictureUtil;
import com.purong.purong.widget.IdentityDialog;
import com.purong.purong.widget.LoadingDialog;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;

import java.io.File;
import java.util.List;

/**
 * Created by wenjiezeng on 2017/11/13.
 * 极速贷-认证材料-身份认证
 */
public class SpeedMaterIdentActivity extends BasePermActivity{
    public static final int IDENTITY_FRONT=0X001,IDENTITY_AFTER=0X002,IDENTITY_HEAD=0X002;
    private RelativeLayout frontLay,afterLay, headLay;
    private TextView frontToast, afterToast, headToast;
    private RadiusImageView frontImg,afterImg,headImg;
    private int type;
    private LoadingDialog mloadDialog;
    private IdentityDialog frontDialog;
    private TextView nameTv,identityTv;
    private String frontImgPath ="", afterImgPath ="";
    private String frontImgUrl , afterImgUrl;
    private TakePictureUtil takePictureUtil;
    @Override
    public int getRootViewId() {
        return R.layout.activity_speed_materials_identity;
    }

    @Override
    public void initView() {
        showTitle("身份认证");
        showBack();
        showRightTv("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("提交");
            }
        });
        frontLay=(RelativeLayout)findViewById(R.id.identity_front_lay);
        afterLay=(RelativeLayout)findViewById(R.id.identity_after_lay);
        headLay =(RelativeLayout)findViewById(R.id.identity_head_lay);
        int width=(int)(CommonUtil.getScreenWidth(this)*0.578);
        int height=(int)(width*0.631);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(width,height);
        frontLay.setLayoutParams(lp);
        afterLay.setLayoutParams(lp);
        headLay.setLayoutParams(lp);
        frontImg=(RadiusImageView) findViewById(R.id.front_img);
        afterImg=(RadiusImageView) findViewById(R.id.after_img);
        headImg=(RadiusImageView) findViewById(R.id.head_img);

        frontToast =(TextView)findViewById(R.id.ident_front_toast);
        SpannableStringBuilder frontSpan = new SpannableStringBuilder(frontToast.getText());
        frontSpan.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (frontDialog==null){
                    frontDialog=new IdentityDialog(SpeedMaterIdentActivity.this);
                }
                frontDialog.showFront();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.blue));
                ds.setUnderlineText(false);
            }
        },16, frontToast.getText().length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        frontToast.setMovementMethod(LinkMovementMethod.getInstance());
        frontToast.setHighlightColor(Color.TRANSPARENT);
        frontToast.setText(frontSpan);

        afterToast =(TextView)findViewById(R.id.ident_after_toast);
        SpannableStringBuilder afterSpan = new SpannableStringBuilder(afterToast.getText());
        afterSpan.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (frontDialog==null){
                    frontDialog=new IdentityDialog(SpeedMaterIdentActivity.this);
                }
                frontDialog.showAfter();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.blue));
                ds.setUnderlineText(false);
            }
        },19, afterToast.getText().length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        afterToast.setMovementMethod(LinkMovementMethod.getInstance());
        afterToast.setHighlightColor(Color.TRANSPARENT);
        afterToast.setText(afterSpan);

        headToast =(TextView)findViewById(R.id.head_toast);
        SpannableStringBuilder portraitSpan = new SpannableStringBuilder(headToast.getText());
        portraitSpan.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (frontDialog==null){
                    frontDialog=new IdentityDialog(SpeedMaterIdentActivity.this);
                }
                frontDialog.showHead();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.blue));
                ds.setUnderlineText(false);
            }
        },11, headToast.getText().length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        headToast.setMovementMethod(LinkMovementMethod.getInstance());
        headToast.setHighlightColor(Color.TRANSPARENT);
        headToast.setText(portraitSpan);
    }

    @Override
    public void initData() {

    }

    @Override
    protected String[] getPermissions() {
        return PermissionConstant.PERMS_PHOTO;
    }

    @Override
    protected void doPermissinosSuc() {

    }
    private void initPictureUtil(){
        takePictureUtil=new TakePictureUtil(this, new TakePictureUtil.OnTakePictureListenr() {
            @Override
            public void onCameraResult(File file) {
                GlideUtil.getInstance().loadFile(SpeedMaterIdentActivity.this,file,frontImg,false,true);
            }

            @Override
            public void onAlbumResult(List<String> files) {
            }

            @Override
            public void onAlbumCutResult(File file) {
            }

            @Override
            public void onCameraCutResult(File file) {
            }

            @Override
            public void onFail() {
            }
        });
        takePictureUtil.needCut(true);
        takePictureUtil.album(5);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (takePictureUtil!=null){
            takePictureUtil.onActivityResult(requestCode,resultCode,data);
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.identity_front_lay:
                type=IDENTITY_FRONT;
                if (Build.VERSION.SDK_INT >= 23) {
                    checkPermission();
                } else {
                    takePictureUtil=new TakePictureUtil(this, new TakePictureUtil.OnTakePictureListenr() {
                        @Override
                        public void onCameraResult(File file) {
                            showToast("onCameraResult");
                            GlideUtil.getInstance().loadFile(SpeedMaterIdentActivity.this,file,frontImg,false,true);
                        }

                        @Override
                        public void onAlbumResult(List<String> files) {
                            showToast("onAlbumResult");
                        }

                        @Override
                        public void onAlbumCutResult(File file) {
                            showToast("onAlbumCutResult");
                            GlideUtil.getInstance().loadFile(SpeedMaterIdentActivity.this,file,frontImg,false,true);
                        }

                        @Override
                        public void onCameraCutResult(File file) {
                            showToast("onCameraCutResult");
                            GlideUtil.getInstance().loadFile(SpeedMaterIdentActivity.this,file,frontImg,false,true);

                        }

                        @Override
                        public void onFail() {
                            showToast("onFail");

                        }
                    });
                    takePictureUtil.needCut(true);
                    takePictureUtil.camera();
                }
                break;
            case R.id.identity_after_lay:
                break;
            case R.id.identity_head_lay:
                break;
        }
    }
}
